package com.easyaop.api.advisor;

import com.easyaop.api.advisor.aspectj.CustomPointcutExpression;
import com.easyaop.api.advisor.aspectj.CustomPointcutParser;
import com.easyaop.api.pointcut.ClassObject;
import com.easyaop.api.pointcut.MethodObject;
import com.easyaop.api.pointcut.Pointcut;

public class AspectjExpressionPointcut extends Pointcut {
    protected static final CustomPointcutParser PARSER =
            CustomPointcutParser.getPointcutParserSupportingAllPrimitives();

    protected String expression;

    protected CustomPointcutExpression customPointcutExpression;

    public AspectjExpressionPointcut(String expression) {
        super(expression);
        this.expression = expression;
        this.customPointcutExpression = PARSER.parsePointcutExpression(expression);
    }

    public static AspectjExpressionPointcut of(String expression) {
        return new AspectjExpressionPointcut(expression);
    }

    @Override
    public boolean isClass(ClassObject clz) {
        return customPointcutExpression.couldMatchJoinPointsInType(clz.getName());
    }

    @Override
    public boolean isMethod(MethodObject method) {
        return customPointcutExpression.matchesMethodExecution(method.getClassName(), method.getName(),
                method.getDescriptor());
    }
}
